<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-button
          type="primary"
          plain
          size="small"
          @click="isShow"
          class="btnNew"
          v-no-more-click
          >新增公司</el-button
        >
        <div class="listUl">
          <ul>
            <li
              v-for="(item, index) in data"
              :key="index"
              @click="companyId(item, index)"
              :class="{ bg: index == isactive }"
            >
              {{ item.companyName }}
            </li>
          </ul>
        </div></el-col
      >
      <el-col :span="12"
        ><div>
          <el-form
            label-width="80px"
            :model="form"
            status-icon
            ref="form"
            :rules="rules"
          >
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="close" v-no-more-click>取 消</el-button>
            <el-button type="primary" @click="company" v-no-more-click>提交</el-button>
            <p style="margin-top: 10px">
              如此公司下已无部门，您可以<u class="btnDelette" @click="deldialog"
                >删除此公司</u
              >
            </p>
          </span>
        </div></el-col
      >
    </el-row>
    <el-dialog
      title="提示"
      :visible.sync="dialog"
      width="20%"
      :modal-append-to-body="false"
      :append-to-body="true"
      :close-on-click-modal="false"
    >
      <span>是否删除名字为{{ companytext }}的公司</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog = false" v-no-more-click>取 消</el-button>
        <el-button type="primary" @click="delCompany" v-no-more-click>确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { pagelist, companyAdd, delCompany } from "../../../src/assets/js/api";
export default {
  name: "Organza",
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      isactive: null,
      dialog: false,
      companytext: "",
      update: true,
      add: false,
      form: {
        companyName: "",
        id: ""
      },
      data: [],
      // 选中公司id
      id: undefined,
      rules: {
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" }
          // {
          //   min: 2,
          //   max: 12,
          //   message: "长度在 2 到 12 个字符",
          //   trigger: "change"
          // }
        ]
      }
    };
  },
  created() {
    this.getlistData();
  },
  methods: {
    getlistData() {
      pagelist().then(res => {
        this.data = res.data;
      });
    },
    isShow() {
      this.form.id = null;
      this.form.companyName = "";
      this.isactive = null;
    },
    company() {
      this.$refs.form.validate().then(() => {
        let form = {};
        form = JSON.stringify(this.form);
        companyAdd(form).then(res => {
          if (res.code === 200) {
            this.getlistData();
            this.$message({
              message: res.message,
              type: "success"
            });
            this.$emit("gitlist");
            this.$emit("dialogVisible", false);
            this.$refs.form.resetFields();
          } else {
            this.$message({
              message: res.message,
              type: "error"
            });
            this.$emit("dialogVisible", false);
            this.$refs.form.resetFields();
          }
        });
      });
    },
    // 获取公司id
    companyId(item, index) {
      this.companytext = item.companyName;
      this.isactive = index;
      this.id = item.id;
      this.form.companyName = item.companyName;
      this.form.id = item.id;
    },
    // 打开公司弹框
    deldialog() {
      this.dialog = true;
      if (this.id === undefined) {
        this.dialog = false;
        this.$message({
          message: "请选择需要删除的公司",
          type: "warning"
        });
      }
    },
    // 删除公司
    delCompany() {
      this.deldialog();
      delCompany(this.id).then(res => {
        if (res.code === 200) {
          this.dialog = false;
          this.$message({
            message: res.message,
            type: "success"
          });
          this.getlistData();
          this.$emit("gitlist");
          this.$emit("dialogVisible", false);
          this.$refs.form.resetFields();
        } else {
          this.dialog = false;
          this.$message({
            message: res.message,
            type: "danger"
          });
          this.getlistData();
          this.$emit("gitlist");
          this.$emit("dialogVisible", false);
          this.$refs.form.resetFields();
        }
      });
    },
    close() {
      this.$emit("dialogVisible", false);
      this.$emit("gitlist");
    }
  }
};
</script>

<style scoped type="less">
ul {
  padding: 5px;
  height: 200px;
  overflow: auto;
  background-color: rgb(241, 243, 244);
}
li {
  line-height: 35px;
}
li:hover {
  cursor: pointer;
}
.bg {
  background: #409eff;
  color: #fff;
}
.btnDelette {
  color: rgb(28, 158, 254);
  cursor: pointer;
}
.btnDelette:hover {
  color: red;
}
.btnNew {
  position: absolute;
  top: -36px;
  left: 0;
}
</style>
